﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Slot : MonoBehaviour
{
    public enum State {
        none,
        empty,
        player,
        enemy,
        enemy2,
        boss1,
        treasure,
        box,
    };
    public GridManager gridManager;
    public Vector2 GridPosition;
    public State SlotState;
    public SlotObject SlotObject;
    public SpriteRenderer SR;

    void Awake() {
        SR = GetComponent<SpriteRenderer>();
        SlotState = State.none;
    }

    public void Clear() {
        if(SlotObject == null)
            return;
        Destroy(SlotObject.gameObject);
        SlotObject = null;
    }

    public void SetEmpty() {
        SlotState = State.empty;
        Clear();
        SR.sprite = gridManager.EmptySprite;
    }

    public void SetNone() {
        SlotState = State.none;
        Clear();
        SR.sprite = gridManager.NoneSprite;
    }

    public void SetPlayer(GameObject PlayerO) {
        SetEmpty();
        SlotObject = Instantiate(PlayerO, transform).GetComponent<SlotObject>();
        SlotObject.slot = this;
        SlotObject.gameObject.SetActive(true);
        SlotState = State.player;
    }

    public void SetEnemy(GameObject EnemyO) {
        SetEmpty();
        SlotObject = Instantiate(EnemyO, transform).GetComponent<SlotObject>();
        SlotObject.slot = this;
        SlotObject.gameObject.SetActive(true);
        SlotState = State.enemy;
    }

    public void SetBoss1(GameObject Boss1O) {
        SetEmpty();
        SlotObject = Instantiate(Boss1O, transform).GetComponent<SlotObject>();
        SlotObject.slot = this;
        SlotObject.gameObject.SetActive(true);
        SlotState = State.boss1;
        GetNewSlot(1, 1).SlotState = State.boss1;
        GetNewSlot(1, 0).SlotState = State.boss1;
        GetNewSlot(1, -1).SlotState = State.boss1;
        GetNewSlot(0, 1).SlotState = State.boss1;
        GetNewSlot(0, -1).SlotState = State.boss1;
        GetNewSlot(-1, 1).SlotState = State.boss1;
        GetNewSlot(-1, 0).SlotState = State.boss1;
        GetNewSlot(-1, -1).SlotState = State.boss1;
    }

    public void SetBox(GameObject BoxO) {
        SetEmpty();
        SlotObject = Instantiate(BoxO, transform).GetComponent<SlotObject>();
        SlotObject.slot = this;
        SlotObject.gameObject.SetActive(true);
        SlotState = State.box;
    }

    public void SetTreasure(GameObject TreasureO) {
        SetEmpty();
        SlotObject = Instantiate(TreasureO, transform).GetComponent<SlotObject>();
        SlotObject.slot = this;
        SlotObject.gameObject.SetActive(true);
        SlotState = State.treasure;
    }

    public Slot GetNewSlot(int dx, int dy) {
        dx += (int)(GridPosition.x + 0.1f);
        dy += (int)(GridPosition.y + 0.1f);
        if(dx < 0 || dx >= gridManager.Width || dy < 0 || dy >= gridManager.Height)
            return null;
        return gridManager.Grids[dx, dy];
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
